/*
 * HoangTN
 * servlet controller for log01020.jsp
 */
package vn.edu.fpt.xml.hrm.controller.timesheet;

import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;
import javax.xml.stream.XMLStreamException;
import org.w3c.dom.Document;
import vn.edu.fpt.xml.hrm.controller.AbstractController;
import vn.edu.fpt.xml.hrm.model.ActivityBean;
import vn.edu.fpt.xml.hrm.pojo.Activities;
import vn.edu.fpt.xml.hrm.pojo.Activity;
import vn.edu.fpt.xml.hrm.pojo.Timesheet;

/**
 *
 * @author HoangTN
 */
public class AddTimesheetController extends AbstractController {

    private final String homePage = "jsp/log01020.jsp";
    private final String errorPage = "jsp/error.jsp";
    private boolean found;

    @Override
    protected String execute() {

        HttpServletRequest request = this.getRequest();
        HttpServletResponse response = this.getResponse();

        String webPath = getServletContext().getRealPath("/");
        String filePath = webPath + "WEB-INF/activities.xml";

        Document doc = TimeSheetUtil.createDOMTree(filePath);

        String url = errorPage;
        String action = request.getParameter("action");

        if (action.equals("Submit")) {
            String[] dates = request.getParameterValues("txtDate");
            String[] descriptions = request.getParameterValues("txtDescription");
            String[] projects = request.getParameterValues("cboProject");
            String[] durations = request.getParameterValues("txtDuration");
            String emp = request.getSession().getAttribute("EMPLOYEEID").toString();
            List<Activity> list = new ArrayList<Activity>();
            for (int i = 0; i < dates.length; i++) {
                if (dates[i] != "" && !dates[i].isEmpty()) {
                    try {
                        Activity item = new Activity();
                        DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
                        Date date2 = formatter.parse(dates[i]);
                        GregorianCalendar calendar = new GregorianCalendar();
                        calendar.setTime(date2);
                        XMLGregorianCalendar xmlCalendar = DatatypeFactory.newInstance().newXMLGregorianCalendar(calendar);
                        item.setDate(xmlCalendar);
                        item.setEmployee(emp);
                        item.setDescription(descriptions[i]);
                        item.setProject(projects[i]);
                        item.setDuration(Double.valueOf(durations[i]));
                        list.add(item);
                    } catch (DatatypeConfigurationException ex) {
                        ex.printStackTrace();
                    } catch (ParseException ex) {
                        ex.printStackTrace();
                    }
                }
            }
            Activities results = new Activities();
            results.setActivity(list);
            //Marshall.marshall(filePath, results);
            ActivityBean bean = new ActivityBean();
            boolean done = bean.insertNewActivities(results);
            if (done) {
                try {
                    request.setAttribute("checked", "checked");
                    url = homePage;
                    Timesheet ts = new Timesheet();
                    ts.createXMLTimeSheet(webPath + "WEB-INF/timesheet.xml");
                } catch (IOException ex) {
                    Logger.getLogger(AddTimesheetController.class.getName()).log(Level.SEVERE, null, ex);
                } catch (XMLStreamException ex) {
                    Logger.getLogger(AddTimesheetController.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
        return url;
    }
}
